package com.fanshuai.soring.boot.webflux.metrics;

import io.prometheus.client.Collector;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.exporter.common.TextFormat;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Mono;

import java.io.StringWriter;
import java.util.Enumeration;

@Slf4j
public class ReactiveMetricsOutput {
    public Mono<String> metrics() {
        StringWriter writer = new StringWriter();
        Enumeration<Collector.MetricFamilySamples> samplesEnumeration = CollectorRegistry.defaultRegistry.metricFamilySamples();

        return Mono.fromCallable(() -> {
            try {
                TextFormat.write004(writer, samplesEnumeration);
            } catch (Exception e) {
                log.error(e + "");
            }
            return writer.toString();
        });
    }
}
